package com.nest4cloud.nest.account.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nest4cloud.nest.account.entity.AccountsEntity;
import com.nest4cloud.nest.common.core.util.R;
import com.nest4cloud.nest.common.log.annotation.SysLog;
import com.nest4cloud.nest.common.security.util.SecurityUtils;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.nest4cloud.nest.account.entity.AccTransactionsEntity;
import com.nest4cloud.nest.account.service.AccTransactionsService;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import com.nest4cloud.nest.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 账单表
 *
 * @author wens
 * @date 2024-11-26 21:14:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accTransactions" )
@Tag(description = "accTransactions" , name = "账单表管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccTransactionsController {

    private final  AccTransactionsService accTransactionsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param accTransactions 账单表
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @HasPermission("account_accTransactions_view")
    public R getAccTransactionsPage(@ParameterObject Page page, @ParameterObject AccTransactionsEntity accTransactions) {
        LambdaQueryWrapper<AccTransactionsEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(accTransactions.getAccountId()),AccTransactionsEntity::getAccountId,accTransactions.getAccountId());
		wrapper.eq(StrUtil.isNotBlank(accTransactions.getTransactionType()),AccTransactionsEntity::getTransactionType,accTransactions.getTransactionType());
        wrapper.eq(AccTransactionsEntity::getUserId, SecurityUtils.getUser().getId());
        return R.ok(accTransactionsService.pageEntity(page, wrapper));
    }




    /**
     * 通过条件查询账单表
     * @param accTransactions 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping("/details" )
    @HasPermission("account_accTransactions_view")
    public R getDetails(@ParameterObject AccTransactionsEntity accTransactions) {
        return R.ok(accTransactionsService.list(Wrappers.query(accTransactions)));
    }

    @Operation(summary = "查询资产下的账单记录", description = "根据资产ID查询账单消费记录")
    @GetMapping("/queryTransactionsByAccount")
    public R queryTransactionsByAccount(@ParameterObject AccTransactionsEntity accTransactions) {
        return R.ok(accTransactionsService.queryTransactionsByAccount(accTransactions));
    }

    /**
     * 新增账单表
     * @param accTransactions 账单表
     * @return R
     */
    @Operation(summary = "新增账单表" , description = "新增账单表" )
    @SysLog("新增账单表" )
    @PostMapping
    @HasPermission("account_accTransactions_add")
    public R save(@RequestBody AccTransactionsEntity accTransactions) {
        return R.ok(accTransactionsService.saveEntity(accTransactions));
    }

    /**
     * 修改账单表
     * @param accTransactions 账单表
     * @return R
     */
    @Operation(summary = "修改账单表" , description = "修改账单表" )
    @SysLog("修改账单表" )
    @PutMapping
    @HasPermission("account_accTransactions_edit")
    public R updateById(@RequestBody AccTransactionsEntity accTransactions) {
        return R.ok(accTransactionsService.updateById(accTransactions));
    }

    /**
     * 通过id删除账单表
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除账单表" , description = "通过id删除账单表" )
    @SysLog("通过id删除账单表" )
    @DeleteMapping
    @HasPermission("account_accTransactions_del")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(accTransactionsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param accTransactions 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @HasPermission("account_accTransactions_export")
    public List<AccTransactionsEntity> exportExcel(AccTransactionsEntity accTransactions,Long[] ids) {
        return accTransactionsService.list(Wrappers.lambdaQuery(accTransactions).in(ArrayUtil.isNotEmpty(ids), AccTransactionsEntity::getId, ids));
    }

    /**
     * 导入excel 表
     * @param accTransactionsList 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @HasPermission("account_accTransactions_export")
    public R importExcel(@RequestExcel List<AccTransactionsEntity> accTransactionsList, BindingResult bindingResult) {
        return R.ok(accTransactionsService.saveBatch(accTransactionsList));
    }
}
